package controller;

import model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.*;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决请求代码问题
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //获取表单的数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //访问数据库：JDBC技术处理
        User loginUser = new User();
        loginUser.setUsername(username);
        loginUser.setPassword(password);
        //响应用户的请求信息
        try {
            //加载drive类
            Class.forName("com.mysql.cj.jdbc.Driver");

            //获取数据库链接
            String url = "jdbc:mysql://localhost:3306/blog_db?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true";
            String user = "root";
            String pwd = "123456";
            Connection conn = DriverManager.getConnection(url, user, pwd);
            String sql = "select * from users where username ='" + username + "'";
            //创建执行语句
            Statement stmt = conn.createStatement();
            //执行sql语句 ：查询功能：executequery 结果就Resultset，相当于是一个指针，指向结果表的第一条记录之前。
            ResultSet rs = stmt.executeQuery(sql);
            User resultUser = new User();
            //处理结果
            HttpSession session = req.getSession();
            if (rs.next()) {
                //该用户已经是存在的，进行判断密码是否准确
                //把数据库的数据表的查询结果封装成对象。
                resultUser.setUsername(rs.getString("username"));
                resultUser.setPassword(rs.getString("password"));
                //判断登录用户的密码和查询结果的密码进行匹配，如果正确，则把登录用户写入到共享域中并跳转到index.jsp
//                否则提示用户输入错误信息：密码错误
//                req.setAttribute("loginUser",loginUser);
//                req.getRequestDispatcher("index.jsp").forward(req,resp);

            } else {
                //提示错误信息：该用户不存在，请重新输入用户名，并重新定位到login.jsp界面
                req.setAttribute("error", "该用户不存在，请重新输入用户名！");
                req.getRequestDispatcher("login.jsp").forward(req, resp);
            }
            if (password.equals(resultUser.getPassword())){
                req.setAttribute("loginUser",loginUser);
                session.setAttribute("user",loginUser);
                req.getRequestDispatcher("index.jsp").forward(req,resp);
            }
            else{
                req.setAttribute("error","密码错误！请重新输入");
                req.getRequestDispatcher("login.jsp").forward(req,resp);
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
